package com.ks.fission.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.dto.SearchCriteriaBase;
import com.ks.core.util.ToolUtil;
import com.ks.fission.controller.dto.TaskMemberRequest;
import com.ks.fission.dao.BFissionTaskMapper;
import com.ks.fission.entity.BFissionTask;
import com.ks.fission.entity.BFissionTaskMember;
import com.ks.fission.dao.BFissionTaskMemberMapper;
import com.ks.fission.entity.vo.BFissionTaskMemberInfo;
import com.ks.fission.entity.vo.BFissionTaskMemberParam;
import com.ks.fission.service.IBFissionTaskMemberService;
import com.ks.fission.service._ServiceBase;
import io.swagger.models.auth.In;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 裂变-会员分享记录 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-21
 */
@Service
public class BFissionTaskMemberServiceImpl extends _ServiceBase<BFissionTaskMemberMapper, BFissionTaskMember> implements IBFissionTaskMemberService {

    @Resource
    private BFissionTaskMemberMapper bFissionTaskMemberMapper;

    @Resource
    private BFissionTaskMapper bFissionTaskMapper;

    @Override
    public Map<String, Object> selectIBFissionTaskMember(OperationContext context, SearchCriteriaBase searchCriteriaBase) {
        ArrayList<SearchCriteriaBase.matchField> fields = searchCriteriaBase.getFields();
        BFissionTaskMemberParam bFissionTaskMemberParam = new BFissionTaskMemberParam();
        if (ToolUtil.isNotEmpty(fields)) {
            for (SearchCriteriaBase.matchField field : fields) {
                if ("name".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        bFissionTaskMemberParam.setINickName(field.getValue());
                    }
                }
                if ("receiveTime".equals(field.getName())) {
                    if (ToolUtil.isNotEmpty(field.getValue())) {
                        String[] time = field.getValue().split(",");
                        bFissionTaskMemberParam.setStartTime(time[0] + " 00:00:00");
                        bFissionTaskMemberParam.setEndTime(time[1] + " 23:59:59");
                    }
                }
            }
        }

        if (!isSystem(context)) {
            bFissionTaskMemberParam.setOrgID(context.getOrgId().intValue());
            bFissionTaskMemberParam.setOrgSubID(context.getOrgSubId().intValue());
            bFissionTaskMemberParam.setTenantID(context.getTenantId().intValue());
        }
        Page limit = new Page(searchCriteriaBase.getPageIndex(), searchCriteriaBase.getLimit());
        List<BFissionTaskMemberInfo> bFissionTaskMemberInfos = bFissionTaskMemberMapper.taskMemberInfo(limit, bFissionTaskMemberParam);
        Map<String, Object> resultMap = this.isResultMap(bFissionTaskMemberInfos, searchCriteriaBase);
        Integer count = bFissionTaskMemberMapper.taskMemberInfoCount(bFissionTaskMemberParam);
        resultMap.put("count", count);
        return resultMap;
    }

    @Override
    public int addTaskMember(OperationContext context, TaskMemberRequest taskMemberRequest) {
        BFissionTaskMember fissionTaskMember = getBFissionTaskMember(context.getTenantId().intValue(), context.getUserId().intValue(), taskMemberRequest.getITaskId());
        if (ToolUtil.isEmpty(fissionTaskMember)){
            BFissionTask bFissionTask = bFissionTaskMapper.selectById(taskMemberRequest.getITaskId());
            bFissionTask.setIReceives(bFissionTask.getIReceives() + 1);
            Integer code = bFissionTaskMapper.updateById(bFissionTask);
            if (code > 0) {
                BFissionTaskMember bFissionTaskMember = new BFissionTaskMember();
                bFissionTaskMember.setIMemberId(context.getUserId().intValue());
                bFissionTaskMember.setITaskId(taskMemberRequest.getITaskId());
                bFissionTaskMember.setICode(taskMemberRequest.getICode());
                bFissionTaskMember.setIReceiveTime(new Date());
                bFissionTaskMember.setCreateUserID(context.getUserId().intValue());
                bFissionTaskMember.setDataType("TENANT");
                bFissionTaskMember.setOrgID(context.getOrgId().intValue());
                bFissionTaskMember.setOrgSubID(context.getOrgSubId().intValue());
                bFissionTaskMember.setTenantID(context.getTenantId().intValue());
                Integer i = bFissionTaskMemberMapper.insert(bFissionTaskMember);
                return i;
            }
            return code;
        }
        return 1;
    }

    public BFissionTaskMember getBFissionTaskMember(int tenantId, int MemberId, int TaskId) {
        BFissionTaskMember bFissionTaskMember = new BFissionTaskMember();
        bFissionTaskMember.setIMemberId(MemberId);
        bFissionTaskMember.setITaskId(TaskId);
        bFissionTaskMember.setTenantID(tenantId);
        bFissionTaskMember.setDelMark(Const.DEL_FLAG_NORMAL);
        BFissionTaskMember taskMember = bFissionTaskMemberMapper.selectOne(bFissionTaskMember);
        return taskMember;
    }
}
